
<html>
<head>
    <title>考核指标配置</title>
    <link rel="stylesheet" type="text/css" href="/META-INF/resources/static/devtools30/js/bootstrap-3.3.5/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="/META-INF/resources/static/devtools30/js/jquery-easyui-1.5/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/META-INF/resources/static/devtools30/js/jquery-easyui-1.5/themes/icon.css">
    <link rel="stylesheet" href="/META-INF/resources/static/devtools30/js/plugins/validationEngine/css/validationEngine.jquery.css" type="text/css"/>
    <link rel="stylesheet" type="text/css" href="/META-INF/resources/static/devtools30/css/styledevtools.css">
    <link rel="stylesheet" href="/META-INF/resources/static/devtools30/js/plugins/layer/skin/layer.css" />
    <link rel="stylesheet" type="text/css" href="/static/portalres/jsfolder/htmlxjs/skins/dhtmlxgrid.css"/>
    <link rel="stylesheet" type="text/css" href="/static/portalres/jsfolder/htmlxjs/skins/dhtmlxgrid_pgn_bricks.css"/>

    <script type="text/javascript" src="/static/portalres/jsfolder/htmlxjs/dhtmlxcommon.js"></script>
    <script type="text/javascript" src="/static/portalres/jsfolder/htmlxjs/dhtmlxcontainer.js"></script>
    <script type="text/javascript" src="/static/portalres/jsfolder/htmlxjs/dhtmlxgrid.js"></script>
    <script type="text/javascript" src="/static/portalres/jsfolder/htmlxjs/dhtmlxgridcell.js"></script>
    <script type="text/javascript" src="/static/portalres/jsfolder/htmlxjs/dhtmlxgrid_filter.js"></script>
    <script type="text/javascript" src="/static/portalres/jsfolder/htmlxjs/dhtmlxgrid_pgn.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/bootstrap-3.3.5/bootstrap.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/jquery-easyui-1.5/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/jquery-easyui-1.5/datagrid-filter.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/jquery-easyui-1.5/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/my97DatePicker/WdatePicker.js" ></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/plugins/layer/layer.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/easyuiPager.js?=5"></script>

    <script src="/META-INF/resources/static/devtools30/js/plugins/validationEngine/jquery.validationEngine.js" type="text/javascript" charset="utf-8"></script>
    <script src="/META-INF/resources/static/devtools30/js/plugins/validationEngine/languages/jquery.validationEngine-zh_CN.js" type="text/javascript" charset="utf-8">
    </script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/datagrid-dnd.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/FormValidation.js"></script>

    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/commondevtools.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/usetools/dldevusetool.js"></script>
    <script type="text/javascript" src="/META-INF/resources/static/devtools30/js/jquery.cookie.js"></script>

    <script>
        var zztypeid;
        var itemid;
        var pid;
        //增加考核页面api中表id
        var itemFormId = "3e4324b5-b083-4ca5-ad18-86cf14ba3d26";

        var height = findDimensions_height();
        var width = findDimensions_width();
        $(function () {
            $("#l_datagrid").height(height - 50);
            $("#r_datagrid").height(height - 50);

            loadKaoHeFangAnList();
            loadZbList();
        })

        function loadKaoHeFangAnList() {
            var url = "kaoheZhibiaoselZzData.json";
            var url1 = "kaoheZhibiaoselZzTeamData.json";
            var postdata = {};
            dLongDtool.newpostajax(url, postdata, function (data) {
                if (data.success) {
                    var griddata = {};
                    griddata.rows = [];
                    $.each(data.Table, function (i, item) {
                        var newObject = jQuery.extend(true, {}, item);
                        griddata.rows.push({"id":newObject.id,"text":newObject.text,"pid":""});
                    });
                    dLongDtool.newpostajax(url1, postdata, function (khdata) {
                        $.each(khdata.Table, function (i, item) {
                            var newObject = jQuery.extend(true, {}, item);
                            for (var x = 0; x < griddata.rows.length; x++) {
                                if (newObject.zztypeid==griddata.rows[x].id) {

                                    griddata.rows.push({"id":newObject.id,"text":newObject.definename,"pid":newObject.zztypeid});
                                }
                            }
                        });
                        var fileds = "id,text,pid";
                        //根据pid拼接成tree的json
                        var nodes = ConvertToTreeGridJson(griddata.rows, "id", "pid", fileds);
                        $("#l_datagrid").tree({
                            method: "get",
                            animate: true,
                            data: nodes,
                            onClick: function (node) {
                                if(node.pid!=""){
                                    zztypeid = node.id;
                                    itemid = node.id;
                                }else{
                                    zztypeid= node.pid;
                                }
                                grid_Load(node.id);


                            }
                        });
                    }, true);
                }
            }, true);


        }

        function loadZbList() {
            var GetGridColumn = [[
                {field: 'id', title: "id", width: '50px', hidden: true, rowspan: "2"},
                {field: 'khname', title: "指标名称", width: '180px', halign: 'center', align: 'left', rowspan: "2"},
                {field: 'cjm', title: '层级码', width: '80px', align: 'center', rowspan: "2"},
                {field: 'bzz', title: '考核标准', width: '80px', halign: 'center', align: 'right', rowspan: "2"},
                {field: 'bzzoper', title: "操作符", width: '60px', align: 'center', rowspan: "2"},
                {field: 'gz1', title: '扣分规则', width: '180px', align: 'center', colspan: 3},
                {field: 'gz2', title: '加分规则', width: '180px', align: 'center', colspan: 3},
                {field: 'gz3', title: '接口绑定', width: '200px', align: 'center', colspan: 2},
                {field: 'sfqy', title: '是否启用', align: 'center', rowspan: "2", width: '80px',formatter: function (value, row) {
                        var btn = '';
                        btn = '<input  type="button" value="禁用" onclick="doIsStop(0,\'' + row.id + '\',\'' + row.SQFL + '\',1)" />';
                        if (value != 1) {
                            btn = '<input id="Button1" type="button" value="启用" onclick="doIsStop(1,\'' + row.id + '\',\'' + row.SQFL + '\',1)" />';
                        }
                        return btn;
                    }
                },
                {
                    field: 'opt', title: '操作', align: 'center', width: '150px', rowspan: "2",formatter: function (value, row) {
                        return '<a href="#" id="edit" onclick="EditXM(\'' + row.id + '\' )">修 改</a>' +
                                ' | <a href="#" id="edit" onclick="Del(\'' + row.id + '\' )" >删 除</a>'+
                                ' | <a href="#" id="edit" onclick="Bangdingzhibiao(\'' + row.id + '\' )" >绑定指标</a>';
                    }
                }
            ]];
            GetGridColumn[1] = [
                {field: 'kfkd', align: 'center', title: '扣分刻度', width: '60px'},
                {field: 'kffz', align: 'center', title: '扣分分值', width: '60px'},
                {field: 'kfsx', align: 'center', title: '扣分上限', width: '60px'},
                {field: 'jfkd', align: 'center', title: '加分刻度', width: '60px'},
                {field: 'jffz', align: 'center', title: '加分分值', width: '60px'},
                {field: 'jfsx', align: 'center', title: '加分上限', width: '60px'},
                {field: 'jkmc', align: 'center', title: '接口名称', width: '100px'},
                {field: 'zbmc', align: 'center', title: '指标名称', width: '100px'}
            ];

            $('#r_datagrid').treegrid({
                idField: 'id',
                treeField: 'khname',
                rownumbers: true,
                striped: true,
                singleSelect: true,
                columns: GetGridColumn,
                onClickRow: function (rowIndex) {
                    pid = rowIndex.id;
                },
                rowStyler: function (value, rec, index) {
                    if (value.isQY == "0")
                        return "color:red;"
                }
            });
        }

        function grid_Load(id) {
            var url = "kaoheZhibiaoItem.json";
            var postdata = {khid: id};
            dLongDtool.newpostajax(url, postdata, function (data) {
                if (data.success) {
                    var griddata = {};
                    griddata.total = data.totalcount;
                    griddata.rows = [];
                    $.each(data.Table, function (i, item) {
                        var newObject = jQuery.extend(true, {}, item);
                        if (newObject.pid == "") {
                            //pid为空串删除pid,父类没有pid
                            delete newObject.pid;
                            griddata.rows.push(newObject);
                        } else {
                            griddata.rows.push(newObject);
                        }
                    });
                    //需要显示的字段
                    var fileds = "id,khname,cjm,bzz,bzzoper,kfkd,kffz,kfsx,jfkd,jffz,jfsx,jkmc,zbmc,sfqy";
                    //根据pid拼接成tree的json
                    var nodes = ConvertToTreeGridJson(griddata.rows, "id", "pid", fileds);
                    griddata.rows = nodes;
                    $('#r_datagrid').treegrid('loadData', griddata);
                }
            }, true);
        }

        //是否启用
        function doIsStop(id) {
            var url = "${devtoolsURL}/commapiV2?p_dm=V_KaoheZhibiaoList_selectZzData";
            var postdata = {};
            dLongDtool.newpostajax(url, postdata, function (data) {
                if (data.success) {

                }
            }, true);
        }

        //修改行数据
        function EditXM(id) {
            layeropen(0.8 * width, height - 100, "修改考核指标", "/V_KaoheZhibiao/toEdit?itemid=" + itemid + "&zztypeid=" + zztypeid+ "&dataid=" + id);
        }

        //删除行数据
        function Del(id) {
            layerConfirm("是否确定删除?", function () {
                var url = "${devtoolsURL}/commapiV2?p_dm=V_KaoheZhibiaoItemList_delete";
                var postdata = {id: id};
                dLongDtool.newpostajax(url, postdata, function (data) {
                    if (data.success) {
                        loadZbList()
                        grid_Load(itemid)
                    }
                }, true);
            });
        }

        //行数据绑定指标
        function Bangdingzhibiao(id) {
            layeropen(0.9 * width, height - 50, "选择指标", "/V_KaoheZhibiao/toSysZB?zbid="+id );
        }

        //删除考核
        function deleteKaoHe() {
            if (itemid) {
                layerConfirm("是否确定删除?", function () {
                    var url = "${devtoolsURL}/commapiV2?p_dm=V_KaoheZhibiaoList_delete";
                    var postdata = {id: itemid};
                    dLongDtool.newpostajax(url, postdata, function (data) {
                        if (data.success) {
                            loadZbList()
                            $('#r_datagrid').treegrid('loadData', []);
                        }
                    }, true);
                });
            } else {
                layerError("请选择一个考核")
            }
        }

        //考核增加
        function addKaoHe() {
            layeropen(0.6 * width, height - 200, "新增考核", "${devtoolsURL}/V_DevtFormDataController/toEdit?formId=" + itemFormId + "&mDataId=");
        }

        //考核修改
        function editKaoHe() {
            if (itemid) {
                var url = "${devtoolsURL}/commapiV2?p_dm=V_KaoheZhibiaoList_edit";
                var postdata = {id: itemid};
                dLongDtool.newpostajax(url, postdata, function (data) {
                    if (data.success) {
                        $.each(data.Table, function (i, item) {
                            var newObject = jQuery.extend(true, {}, item);
                            layeropen(0.6 * width, height - 200, "修改考核", "${devtoolsURL}/V_DevtFormDataController/toEdit?formId="
                            + itemFormId + "&dataId=" + itemid);
                        });
                    }
                }, true);
            } else {
                layerError("请选择需要修改的考核")
            }
        }

        //顶层指标增加方法
        function addTopZB() {
            if (itemid) {
                layeropen(0.8 * width, height - 100, "增加考核指标", "/V_KaoheZhibiao/toEdit?itemid=" + itemid + "&zztypeid=" + zztypeid);
            } else {
                layerError("请选择需要添加的考核")
            }
        }

        //底层指标增加方法
        function addDownZB() {
            if (pid) {
                layeropen(0.8 * width, height - 100, "增加考核指标", "/V_KaoheZhibiao/toEditZB?itemid=" + itemid + "&pid=" + pid + "&zztypeid=" + zztypeid);
            } else {
                layerError("请选择需要添加的指标")
            }
        }

        //刷新方法
        function dorefresh() {
//            reloadKaoHeFangAnList();
            grid_Load(itemid);
        }

        //树形结构数据封装方法
        function ConvertToTreeGridJson(rows, idFieldName, pidFieldName, fileds) {
            function exists(rows, ParentId) {
                for (var i = 0; i < rows.length; i++) {
                    if (rows[i][idFieldName] == ParentId)
                        return true;
                }
                return false;
            }

            var nodes = [];
            // get the top level nodes
            for (var i = 0; i < rows.length; i++) {
                var row = rows[i];
                if (!exists(rows, row[pidFieldName])) {
                    var data = {
                        id: row[idFieldName]
                    }
                    var arrFiled = fileds.split(",");
                    for (var j = 0; j < arrFiled.length; j++) {
                        if (arrFiled[j] != idFieldName)
                            data[arrFiled[j]] = row[arrFiled[j]];
                    }
                    nodes.push(data);
                }
            }

            var toDo = [];
            for (var i = 0; i < nodes.length; i++) {
                toDo.push(nodes[i]);
            }
            while (toDo.length) {
                var node = toDo.shift(); // the parent node
                // get the children nodes
                for (var i = 0; i < rows.length; i++) {
                    var row = rows[i];
                    if (row[pidFieldName] == node.id) {
                        var child = {
                            id: row[idFieldName]
                        };
                        var arrFiled = fileds.split(",");
                        for (var j = 0; j < arrFiled.length; j++) {
                            if (arrFiled[j] != idFieldName) {
                                child[arrFiled[j]] = row[arrFiled[j]];
                            }
                        }
                        if (node.children) {
                            node.children.push(child);
                        } else {
                            node.children = [child];
                        }
                        toDo.push(child);
                    }
                }
            }
            return nodes;
        }

    </script>
</head>
<body>

<div class="navheader form-inline" style="padding:0 5px; height:40px;border-bottom:1px solid  #ccc;width: 100%">
    <div class="btn-group-right">
        <div class="btn-group " role="group">
            <button type="button" class="btn btn-default bi-btn-width140" onclick="addKaoHe()">增加考核</button>
            <button type="button" class="btn btn-default bi-btn-width140" onclick="editKaoHe()">修改考核</button>
            <button type="button" class="btn btn-default bi-btn-width140" onclick="addTopZB()">增加顶层指标</button>
            <button type="button" class="btn btn-default bi-btn-width140" onclick="addDownZB()">增加下级指标</button>
            <button type="button" class="btn btn-default bi-btn-width140" onclick="deleteKaoHe()">删除考核</button>
        </div>
    </div>
</div>

<div style="margin:0 5px; overflow: hidden; " id="caidan">
    <!--表格-->
    <div style="width:20%;float:left;display:inline-block;padding-right:5px;border-right:1px dotted #ccc;">
        <ul id="l_datagrid" class="easyui-tree"></ul>
    </div>
    <!--表格-->
    <div style="width:80%;float:right;display:inline-block;padding-left:5px;">
        <table id="r_datagrid" class="treegrid"></table>
    </div>
</div>
</body>
</html>